package com.gwtt.simulator.netconf.util;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.enums.EnumConverter;

public class XStreamUtil {

    private static final XStream xstream = new XStream();

    static {
        XStream.setupDefaultSecurity(xstream);
        xstream.registerConverter(new EnumConverter());
        xstream.allowTypesByRegExp(new String[]{".*"});
        xstream.ignoreUnknownElements();
    }

    public static String toXML(Object obj) {
        xstream.autodetectAnnotations(true);
        return xstream.toXML(obj);
    }

    @SuppressWarnings("unchecked")
    public static <T> T fromXML(String xml, Class<T> cls) {
        xstream.processAnnotations(cls);
        return (T) xstream.fromXML(xml);
    }

}
